global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/figure_2_share_automation.log, replace name(fig)

capture noi {

    * ----------------------------------------------------------- *
    * Figure: Share of automation patent families in machinery. 
    * ----------------------------------------------------------- *
    * Note that shares are computed for biadic families.

    * Set global color scheme.
    qui include ${code_dir}/config/figuretools.do
    global color cl
    if "${color}" == "cl" {
        colorpalette scico bamO, n(6) saturate(40) intensity(0.9) nograph  
        local cpl_name "_cl"
        local color1 "`r(p1)'"
        local color2 "`r(p2)'"
        local color3 "`r(p3)'"
        local color4 "`r(p4)'"
        local color5 "`r(p5)'"
        local color6 "`r(p6)'"
    }

    * Panel a: Share of automation patents in machinery worldwide. for different thresholds

    * prepare data
    *getting machinery patents only
    use ${final_dir}/family_timeseries_full.dta, clear
    keep if biadic & relevant_field
    keep if year >= 1980 & year <= 2015

    sort year docdb_family_id
    by year: generate n1 = _n
    by year: generate total_pate_peryear_mach = _N
    by year (docdb_family_id): egen total_patent_auto90_mach = sum(auto90)
    by year (docdb_family_id): egen total_patent_auto95_mach = sum(auto95)

    keep year total_pate_peryear_mach total_patent_auto90_mach total_patent_auto95_mach docdb_family_id

    tempfile machinery
    save `machinery'

    use ${final_dir}/family_timeseries_full.dta, clear
    keep if biadic
    keep if year >= 1980 & year <= 2015


    sort year docdb_family_id
    by year: gen total_pate_peryear_all = _N
    by year (docdb_family_id): egen total_patent_auto90_all = sum(auto90)
    by year (docdb_family_id): egen total_patent_auto95_all = sum(auto95)

    *remerging
    mmerge year docdb_family_id using `machinery'
    drop _merge
    drop if total_pate_peryear_mach == .

    keep year total_pate_peryear_all total_patent_auto90_all total_patent_auto95_all total_pate_peryear_mach total_patent_auto90_mach total_patent_auto95_mach 
    duplicates drop

    *generating percentages
    gen share_auto90_1 = total_patent_auto90_mach / total_pate_peryear_mach
    gen share_auto95_1 = total_patent_auto95_mach / total_pate_peryear_mach

    rename share_auto90_1 sh_auto90_mach
    rename share_auto95_1 sh_auto95_mach

    gen percent_auto90_mach = sh_auto90_mach * 100
    gen percent_auto95_mach = sh_auto95_mach * 100

    gen share_auto90_all = total_patent_auto90_all / total_pate_peryear_all
    gen share_auto95_all = total_patent_auto95_all / total_pate_peryear_all
    gen pct_auto90_all = total_patent_auto90_all / total_pate_peryear_all * 100
    gen pct_auto95_all = total_patent_auto95_all / total_pate_peryear_all * 100
    gen pct_mach_all = total_pate_peryear_mach / total_pate_peryear_all * 100
    replace total_patent_auto95_all = total_patent_auto95_all/1000
    replace total_patent_auto90_all = total_patent_auto90_all/1000

    *reporting numbers
    cap log using "${numb_dir}/figure_2_share_automation_numbers.log", replace name(numbers)
    mean(pct_mach_all)
    summarize total_pate_peryear_all , meanonly
    gen total_patents = r(sum)
    summarize total_pate_peryear_mach, meanonly
    gen total_mach = r(sum)
    gen mach_share_alltime = total_mach/total_patents
    mean(pct_mach_all) [aweight= total_pate_peryear_all]
    cap log close numbers

    *two axis version 
    twoway (connected percent_auto90_mach percent_auto95_mach year, lwidth(1.3pt 1.3pt) lpat(dash dash) msym(O D) msize(small small) color("`crm6'" "`crm3'")) ///
    (connected pct_mach_all year, lwidth(1.3pt) color("`color4'") yaxis(2)), ///
    xlabel(1980 (5) 2015, nogrid) ylabel(0 (5) 25, grid glpattern(dash) glwidth(thin) glcolor(gs14)) yscale(r(0 27)) ///
    ytitle("{stSerif:Automation share in machinery (%)}", axis(1)) ytitle("{stSerif: Machinery share in all patents (%)}", axis(2)) xtitle("Year") ///
    text(18 1987 "{stSerif:Auto90 (left axis)}", color(`crm6')) text(10.4 1985.2 "{stSerif:Auto95 (left axis)}", color(`crm3')) ///
    text(23 1990.8 "{stSerif:Machinery (right axis)}", color(`color4'))  ///
    legend(off) yscale(titlegap(*-4)) ylabel(0 (5) 27, axis(2)) ///
    plotregion(margin(b=0 l=0) ) ///
    title("{stSerif:(A)  Worldwide}", position(6)) ///
    name(sh_auto90_auto95_mach1, replace)

    log using "${numb_dir}/figure_2_share_automation_numbers.log", append name(numb)
    *create measure for machinery patents 97-11
    preserve
        gen mach_share = total_pate_peryear_mach/total_pate_peryear_all
        sort year
        by year: list mach_share
        keep if year >= 1997 & year <= 2011
        collapse (sum) total_pate_peryear_mach (sum) total_pate_peryear_all
        gen mach_share_total = total_pate_peryear_mach/total_pate_peryear_all
        list total_pate_peryear_mach total_pate_peryear_all mach_share_total
    restore
    cap log close numb

    * Panel b: Share of automation patents (auto95) in machinery by applicant’s nationality.

    * prepare data
    foreach ctry in US DE FR GB JP { 
        use ${final_dir}/family_timeseries_full.dta, clear
        keep if appl_`ctry' == 1  
        keep if biadic
        keep if relevant_field == 1 
        sort year docdb_family_id
        by year: generate total_pate_peryear_`ctry' = _N
        by year (docdb_family_id): egen total_patent_auto95_`ctry' = sum(auto95)
        gen share_auto95_`ctry' = total_patent_auto95_`ctry' / total_pate_peryear_`ctry'
        gen percent_auto95_`ctry' = share_auto95_`ctry' * 100
        keep year share_auto95_`ctry' percent_auto95_`ctry'
        duplicates drop
        tempfile f_`ctry'
        save `f_`ctry'', replace
    }

    use `f_US', clear
    foreach ctry in DE FR GB JP { 
        mmerge year using `f_`ctry''
        drop _m
    }
    keep year *auto95*
    duplicates drop
    keep if year >= 1980 & year <= 2015

    * plot, save
    if "${color}" == "cl" {
        colorpalette scico bamO, n(6) saturate(40) intensity(0.9) nograph  
        local cpl_name "_cl"
        local color1 "`r(p1)'"
        local color2 "`r(p2)'"
        local color3 "`r(p3)'"
        local color4 "`r(p4)'"
        local color5 "`r(p5)'"
        local color6 "`r(p6)'"
    }

    twoway connected percent_auto95_US percent_auto95_DE percent_auto95_JP percent_auto95_FR percent_auto95_GB year, ///
        lw(1.3pt 1.3pt 1.3pt 1.3pt 1.3pt) ///
        lpat(solid dot dash_dot dash shortdash) ///
        msym(O D T S X) ///
        msize(small small small small small) ///
        xlabel(1980 (5) 2015, nogrid) ///
        ylabel(5 (5) 25, grid glpattern(dash) glwidth(thin) glcolor(gs14) notick) yscale(r(0 27)) ///
        ytitle("Auto95 share in machinery (%)") xtitle("") xscale(r(1978 2016)) ///
        xtitle("Year") ///
        color("`color1'" "`color2'" "`color3'" "`color4'" "`color5'") ///
        text(22.5 2009.8 "{stSerif:United States}", color("`color1'")) ///
        text(17.3 2009 "{stSerif:Germany}", color("`color2'")) ///
        text(16.5 1988.8 "{stSerif:Japan}", color("`color3'")) ///
        text(11.5 1988 "{stSerif:France}", color("`color4'")) ///
        text(6.9 2012 "{stSerif:United Kingdom}", color("`color5'")) ///
        plotregion(margin(b=0 l=0)) ///
        legend(off) ///
        name(sh_auto95_ctry, replace) ///
        title("{stSerif:(B)  Auto95 by country}", position(6))


    * Export figure

    grc1leg2 sh_auto90_auto95_mach1 sh_auto95_ctry, xsize(14) ysize(7) loff
    graph export "${fig_dir}/main/Figure_2_share_automation_combined.pdf", as(pdf) replace

    grc1leg2 sh_auto90_auto95_mach1 sh_auto95_ctry, xsize(14) ysize(7) loff 
    graph export "${fig_dir}/main/fig2.eps", as(eps) replace

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close fig